package com.example.leetcode.leetcode.hot100;

import com.example.leetcode.leetcode.list.ListNode;

/**
 * @author zht
 * @date 2021/1/12 15:33
 */


public class Solution2 {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int temp=0;
        ListNode head = null;
        ListNode p=null;
        while(l1!=null||l2!=null){
            if(l1!=null){
                temp+=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                temp+=l2.val;
                l2=l2.next;
            }
            if(temp>=0){
                int val = temp%10;
                temp=temp/10;
                if(null == head){
                    head=new ListNode(val);
                    p=head;
                }else{
                    ListNode listNode =new ListNode(val);
                    p.next=listNode;
                    p=listNode;
                }
            }
        }
        if (null != p){
            while(temp!=0){
                int val = temp%10;
                temp=temp/10;
                ListNode listNode =new ListNode(val);
                p.next=listNode;
                p=listNode;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(0);
        ListNode head = addTwoNumbers(l1,null);
        while (null!=head){
            System.out.print(head.val);
            head = head.next;
        }
    }
}
